package com.xiaoxi.statemode.frame;

import com.xiaoxi.statemode.context.Context;
import com.xiaoxi.statemode.state.DayState;
import com.xiaoxi.statemode.state.State;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Author xiaoxi
 * @ProjectName design_mode
 * @PackageName com.xiaoxi.statemode.frame
 * @ClassName SafeFrame
 * @Description TODO
 * @Date 2023/3/27 21:28
 * @Version 1.0
 */
public class SafeFrame extends Frame implements ActionListener, Context {

    private TextField textClock = new TextField(60);
    private TextArea textScreen = new TextArea(10, 60);
    private Button use = new Button("使用金库");
    private Button alarm = new Button("按下警铃");
    private Button phone = new Button("正常通话");
    private Button exit = new Button("结束");

    private State state = DayState.getInstance();

    public SafeFrame(String title) throws HeadlessException {
        super(title);
        setBackground(Color.lightGray);
        setLayout(new BorderLayout());

        add(textClock, BorderLayout.NORTH);
        textClock.setEditable(false);

        add(textScreen, BorderLayout.CENTER);
        textScreen.setEditable(false);

        Panel panel = new Panel();
        panel.add(use);
        panel.add(alarm);
        panel.add(phone);
        panel.add(exit);
        add(panel, BorderLayout.SOUTH);

        pack();
        show();


        use.addActionListener(this);
        alarm.addActionListener(this);
        phone.addActionListener(this);
        exit.addActionListener(this);

    }

    @Override
    public void setClock(int hour) {
        String now = "现在时间是：";
        if (hour < 10) {
            now += "0" + hour + ":00";
        } else {
            now += hour + ":00";
        }
        System.out.println(now);
        textClock.setText(now);
        state.doClock(this, hour);
    }

    @Override
    public void changeState(State state) {
        System.out.println("发生了状态咯~~~~~");
        this.state = state;
    }

    @Override
    public void callSecurityCenter(String msg) {
        textScreen.append("call!" + msg + "\n");
    }

    @Override
    public void recordLog(String msg) {
        textScreen.append("record ..." + msg + "\n");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == use) {
            state.doUse(this);
        } else if (e.getSource() == alarm) {
            state.doAlarm(this);
        } else if (e.getSource() == phone) {
            state.doPhone(this);
        } else if (e.getSource() == exit) {
            System.exit(0);
        } else {
            System.out.println("?");
        }
    }
}
